#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <iostream>
#include "engine.h"
#include "image.h"
#include "map.h"

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 500;
const int SCREEN_BPP = 32;
const int BCOL = 10;
const int BROW = 13;

/*
TODO

Shooting and Catching balls..
Grid map making --> See map design for details..

*/


using namespace std;

int main(int argc, char* args[])
{
  SDL_Surface* screen = NULL;
  SDL_Surface* background = NULL;
  PlayerClass Player;
  EventHandler HandleEvent;
  SDL_Event event;
  BallClass Bold[BCOL*BROW+100];
  MapGrid testMap;
  int testInt = 0;
  bool quit, tell_its_working;
 

  /* ======================== Setting up SDL ===================== */
  SDL_Init(SDL_INIT_EVERYTHING);
  screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE);
  if (screen==NULL){
    cout << "ERROR - Screen failed\n"<< flush;
    return 1;
  }
  background = load_bmp("./img/background.bmp");
  SDL_WM_SetCaption("Crazy-Bubble testing Edition",NULL);
  
  

  /* Now we're ready to do some magic =) */


  /* ============== Setting up game engine ==================== */
  /* Setting up background and init player and moves him to start location */
  cout << "Setup done, now starting up game engine =)!\n" << flush;
  apply_surface(0,0,background,screen);
  Player.Init();
  Player.MoveToSurface(320);
  // Testing new Map Class
  testMap.emptyAll();
  testMap.setCol(0, '*');
  testMap.setCol(1, '*');
  testMap.setCol(2, '*');
  testMap.setState(2, 7, 'o');
  testMap.setState(4, 6, '*');
  testMap.PrintMap();
  cout << testMap.getFirstBall(7) << endl << flush;
  
  
  /* ============== New Ball/Map system ==================== */
  
	int i_ball_numb = 0;
  
	for (int iCOL = 0; iCOL < BCOL; iCOL++){
		for (int iROW = 0; iROW < BROW; iROW++){
			if (testMap.getState(iCOL, iROW) == '*'){
			// Draw ball
			Bold[i_ball_numb].Init();
			Bold[i_ball_numb].exsist = true;
			Bold[i_ball_numb].Position.y = iCOL;
			Bold[i_ball_numb].Position.x = iROW;
			Bold[i_ball_numb].Location.x = Bold[i_ball_numb].Position.x * 50;
			Bold[i_ball_numb].Location.y = Bold[i_ball_numb].Position.y * 50;
			Bold[i_ball_numb].DrawBall(screen);
		  	i_ball_numb++;
			}
		}
	}
 
 
	/* ============== End of Mapping ! :) ==================== */
	  
	  
	/* ============== Now we are entering the action part.. ==================== */
  
	Bold[100].Init();
	Bold[100].model = load_bmp("./img/rmbold.bmp");
	for (int i = 41; i < 100; i++) Bold[i].exsist = false;
	Player.Draw(screen);
	Player.BallColor = 0;
	while (quit == false){
  
    while ( SDL_PollEvent(&event)){
      if (event.type == SDL_QUIT)
	{
	  quit = true;
	}
     
	  int i = 0;
      int ballnumb;
      if (event.type == SDL_KEYDOWN){
	switch(event.key.keysym.sym){
	case SDLK_UP:  										// This is going somewhere!
		int xpos, ypos, ballMustBe;
		xpos = testMap.getFirstBall(Player.BallPos);
		ypos = Player.BallPos;
		// Calculate ballpos
		cout << "X : " << xpos << " Y: " << ypos << endl << flush;
		ballMustBe =  (xpos * 13) + ypos;
		cout << "Ball number : " << ballMustBe << endl << flush;
		if ((!Player.gotBall) || (Player.getBallColor() == Bold[ballMustBe].color))
		{
			Bold[ballMustBe].RemoveBall(screen);
			testMap.setState(xpos, ypos, 'o');
			Player.gotBall = true;
			Player.setBallColor(Bold[ballMustBe].color);
		}
	  break;
	case SDLK_DOWN:
		// Working on that
	  break;
	case SDLK_RIGHT:
	  if ((Player.Location.x + Player.model->w )< 620){
	  Player.RemoveTrace(screen);
	  Player.MoveRight();
	  Player.Draw(screen);
	  cout << "Player position : " << Player.BallPos << endl << flush;
	  }
	  break;
	case SDLK_LEFT:
	  if (Player.Location.x > 0 ) {
	  Player.RemoveTrace(screen);
	  Player.MoveLeft();
	  Player.Draw(screen);
	  cout << "Player position : " << Player.BallPos << endl << flush;
	  }
	  break;
	  
	}
      }
      // Again if we get to this point we can say its done!
      // Without spamming..
      if (tell_its_working){
	cout << "Event handler works fine.." << endl << flush;
	tell_its_working = false;
      }
    }  
 }
 
  for (int i=1; i < ROW*COL; i++) {
    Bold[i].Free();
  }

  return 0;
}


